<?php
namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;
use app\app\util\IntUtils;
class  ComputerController extends  BaseController{

    /**
     * @return array
     * 对电脑玩法
     */
    public function match()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $computer['user_id'] = $user_id;
        $computer['role_id'] = input('post.role_id');
        $computer['prop_id'] = input('post.prop_id');
        $computer['position'] = input('post.position');
        $computer['created_at'] = time();
        $computer['score'] = input('post.shoot_score').':'.input('post.gate_score');

        //如果用户position是射门（1），射门分数为3则用户获胜；如果用户position是守门（0），守门分数为3则用户获胜；
        //与电脑对战 赢一局积分加2分，输一局积分加1分
        if( $computer['position'] == 1 ){           //用户射门
            if ( input('post.shoot_score') == 3 ){
                $computer['result'] = 1;            //用户获胜
                $computer['integral'] = 2;          //用户加的积分是2
            }elseif ( input('post.gate_score') == 3 ){
                $computer['result'] = 2;            //用户失败
                $computer['integral'] = 0;          //用户加的积分是0
            }
        }elseif ( $computer['position'] == 0 ){     //用户是守门
            if ( input('post.gate_score') == 3 ){
                $computer['result'] = 1;            //用户获胜
                $computer['integral'] = 2;          //用户加的积分是2
            }elseif ( input('post.shoot_score') == 3 ){
                $computer['result'] = 2;            //用户失败
                $computer['integral'] = 0;          //用户加的积分是0
            }
        }
        //插入数据
        if( DB::name('computer')->insertGetId($computer) ){
            //游戏记录生成后，就在user的游戏次数加1
            Db::query('update ft_user set times = times + 1 where id ='.$computer['user_id']);
            //如果用户获胜在用户表（ft_user）中给该用户的积分加2，如果用户失败，在用户表（ft_user）中给该用户的积分加1
            if( $computer['result'] == 1 ){
                Db::query('update ft_user set integral = integral + 2 ,total_integral = total_integral + 2, victory_times = victory_times + 1 where id ='.$computer['user_id']);
                IntUtils::getUserInt($user_id,2);
            }
//            elseif ( $computer['result'] == 2 ){
//                Db::query('update ft_user set integral = integral + 1,total_integral = total_integral + 1 where id ='.$computer['user_id']);
//                IntUtils::getUserInt($user_id,1);
//            }
            return ft(200,'插入数据成功',$computer);
        }else{
            return ft(402,'插入数据失败');
        }

    }


}